//
//  HLWelcomeController.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/17.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLWelcomeController.h"
#import <UIImageView+WebCache.h>
#import "HLUserAccountViewModel.h"
#import <Masonry.h>

@interface HLWelcomeController ()

@property (nonatomic,strong) UIImageView *backImageView;
@property (nonatomic,strong) UIImageView *iconView;
@property (nonatomic,strong) UILabel *titleLabel;

@end

@implementation HLWelcomeController

- (void)loadView {
    self.view = self.backImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置界面
    [self setupUI];
}

//动画在该方法中执行
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //执行动画
    [self startAnimation];
}

//设置界面
- (void)setupUI {
    //添加子控件
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.titleLabel];
    //添加约束
    //头像
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_bottom).with.offset(-200);
        make.width.mas_equalTo(@90);
        make.height.mas_equalTo(@90);
    }];
    
    //文本
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.top.equalTo(self.iconView.mas_bottom).with.offset(15);
    }];
    
    //隐藏文本
    self.titleLabel.alpha = 0;
}

//开始动画
- (void)startAnimation {
    //修改约束
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view.mas_bottom).with.offset(-500);
    }];
    //执行动画
    [UIView animateWithDuration:1.5 delay:0 usingSpringWithDamping:0.95 initialSpringVelocity:9.5 options:0 animations:^{
        //立即更新约束
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.5 animations:^{
            //显示文本
            self.titleLabel.alpha = 1;
        } completion:^(BOOL finished) {
            //发送通知更改根控制器
            [[NSNotificationCenter defaultCenter] postNotificationName:MainWindowChangeRootControllerNotification object:@"GOTOMainController"];
        }];
    }];
}

#pragma mark - 懒加载
//背景图片
- (UIImageView *)backImageView {
    if (_backImageView == nil) {
        _backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    }
    return _backImageView;
}
//头像
- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] init];
        //设置图片
        [_iconView sd_setImageWithURL:[NSURL URLWithString:[HLUserAccountViewModel getUserImage]] placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
        //设置圆角
        _iconView.layer.cornerRadius = 45;
        //剪切
        _iconView.layer.masksToBounds = YES;
        [_iconView sizeToFit];
        
    }
    return _iconView;
}
//文字
- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithTitle:[NSString stringWithFormat:@"%@ 欢迎回来",[HLUserAccountViewModel getUserName]] withTitleFont:15 withTitleColor:[UIColor lightGrayColor]];
    }
    return _titleLabel;
}

@end
